<?php
App::uses('AppController', 'Controller');
/**
 * Countries Controller
 *
 * @property Country $Country
 * @property PaginatorComponent $Paginator
 */
class CountriesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

	public $paginate = array(
        'limit' 	=> 3,//Nombre max de pays affichés par page
        'order' 	=> array(
            'Country.created' => 'desc'
        )
    );

/**
 * index method
 *
 * @return void
 */
	public function index2() {
		$this->Country->recursive = 0;
		$this->set('countries', $this->Paginator->paginate());
	}

	public function index() {
		
		$this->Country->locale = Configure::read('Config.language');
		//$this->Country->bindTranslation(array('name' => '_name'));
		
		//Pagination
		$this->Paginator->settings = $this->paginate;
		$countries = $this->Paginator->paginate('Country');
		
		$this->set(compact('countries'));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		//$this->Country->locale = Configure::read('Config.languages');

		if ($this->request->is('post')) {
						
			if ($this->Country->saveAssociated($this->request->data)) {
				$this->Session->setFlash(__('The country has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The country could not be saved. Please, try again.'));
			}
		}
		$continents = $this->Country->Continent->find('list');
		$this->set(compact('continents'));
	}	

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Country->locale = Configure::read('Config.languages');

		if (!$this->Country->exists($id)) {
			throw new NotFoundException(__('Invalid country'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Country->saveAssociated($this->request->data)) {
				$this->Session->setFlash(__('The country has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The country could not be saved. Please, try again.'));
			}
		} else {
			$this->Country->id = $id;
			$this->request->data = $this->Country->getAllTranslationsFromDatas();
		}
		$continents = $this->Country->Continent->find('list');
		$this->set(compact('continents'));

	}	

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Country->exists($id)) {
			throw new NotFoundException(__('Invalid country'));
		}
		$options = array('conditions' => array('Country.' . $this->Country->primaryKey => $id));
		$this->set('country', $this->Country->find('first', $options));
	}




/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Country->id = $id;
		if (!$this->Country->exists()) {
			throw new NotFoundException(__('Invalid country'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Country->delete()) {
			$this->Session->setFlash(__('The country has been deleted.'));
		} else {
			$this->Session->setFlash(__('The country could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
